package chatManagers;

/*
 * UserManager.java
 *
 * Created on August 25, 2006, 12:20 PM
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

/**
 *
 * @author spring
 * @version
 */
public class UserManager extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //DBMSManager dbm= new DBMSManager(request.getParameter("db"));
        DBMSManager dbm = new DBMSManager();
        
        //prepare for the AJAX response which will be sent as a string
        response.setContentType("text/plain");
        response.setHeader("Cache-control", "no-cache");
        PrintWriter out = response.getWriter();
        String responseString= new String();
                
        String method = request.getParameter("doAction");
        int uid = new Integer(request.getParameter("userid")).intValue();
        int cid = new Integer(request.getParameter("chatid")).intValue();
        String comment = "";
        
        try{
            if(method.equals("signon")) {
                comment = "<span style=\"color: #009900;\"><i> has joined the chat.</i></span>";
                dbm.AddPost(cid, uid, comment);
                dbm.setUserChat(uid,cid);
            } else if(method.equals("getOwner")) {
                responseString += dbm.getOwner(cid);
            } else if(method.equals("signoff") && cid > 0) {
                // Get the username from the header
                comment = "<span style=\"color: #990000;\"><i> has left the chat.</i></span>";
                dbm.AddPost(cid, uid, comment);
                dbm.unsetUserChat(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbm.closeConnection();
        }
        out.print(responseString);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
